#!/user/bin/env python3
# -*- coding: utf-8 -*-
"""
--------------------------------------
    Author:     JiChao_Song
    Date  :     2020/12/18 18:56
    Desc  :
--------------------------------------
"""
import datetime

from bson import json_util

from application import mongo
from servers.models.ApiCaseSuiteModel import ApiCaseSuite


class CasesRecord(mongo.Document):
    """
    测试结果
    """
    resultObject = mongo.DictField()
    projectId = mongo.StringField()
    casesuiteId = mongo.StringField()
    createData = mongo.IntField()

    def set_case_suite_dict(self):
        """
        设置测试集
        """
        if self.casesuiteId:
            casesuite = ApiCaseSuite.objects(id = self.casesuiteId)
            if casesuite.count() != 0:
                return casesuite.first().to_json()
            return None
        return None

    def to_json(self):

        return {
            '_id': eval(json_util.dumps(self.id)),
            'resultObject': self.resultObject,
            'projectId': self.projectId,
            'casesuiteId': self.casesuiteId,
            'casesuiteObject': self.set_case_suite_dict(),
            'createData': self.createData,
        }